

#---Portmann & Stojanovic 2017: Electoral discrimination against immigrant-origin candidates---#

#-------------------- LOAD DATA

load("PrefVotes.RData")

#-------------- RECODE VARIABLES

  #Negative preference votes

  d$crossoff_pct <- d$totcrossoff/d$WZretour

  #Age: 18-29 Years; 30-49 Years; 50+ Years

  d$age_c3 <- NA
  d$age_c3[d$age>=18 & d$age<=30] <- 0 # 0 < age > 30
  d$age_c3[d$age>=31 & d$age<=50] <- 1 # 30 > age > 50
  d$age_c3[d$age>50] <- 2 # age >=50
  table(d$age, d$age_c3)

  #Party position: Left (AL, GP, SP); Centre (GLP, CVP, FDP); Right (SVP)
  
  d$party_c3 <- mapvalues(d$ListeKurzbez, from = c("SP", "GP", "AL", "GLP", "CVP", "FDP", "SVP"), to = c(0,0,0,1,1,1,2))
  table(d$ListeKurzbez, d$party_c3)
  
  #Name origin (3 categories): Swiss; Non-Swiss Western (Western European, Nordic, English, Southern European); 
  #Non-Swiss non-Western (Hispanic, Eastern European, former Yugoslav, Albanian, Indian, Eastern Asian, Arabs, Central Asian, Turk, other African non-Arab names)
  
  d$origincode_c3 <- NA
  d$origincode[is.na(d$origincode) & d$nonswissnames == 0] <- 0
  d$origincode_c3 <- mapvalues(d$origincode, from = c(0,4,5,6,7,8,9,10,11,12,13,14,15,16), to = c(0,2,2,1,2,2,1,2,2,2,2,2,2,2))
  table(d$origincode, d$origincode_c3)
  
  #Number of candidates (centered)
  d$num_cand <- scale(d$num_cand, center=TRUE, scale=FALSE)
  d$num_cand <- as.vector(d$num_cand)
  
  #change sign of variable (rel_pos *-1)
  d$rel_pos_rc <- d$rel_pos*(-1)
  
  #Name origin (8 categories)
  
  d$origincode_c8 <- NA
  d$origincode[is.na(d$origincode) & d$nonswissnames == 0] <- 0
  d$origincode_c8 <- mapvalues(d$origincode, from = c(0,9,6,5,4,7,8,12,14,10,11,13,15,16), to = c(0,1,2,3,4,5,5,6,6,7,7,7,7,7))
  table(d$origincode_c8)
  
  #Negative preference votes (with reference: all ballots)
  
  d$crossoff_all <- d$totcrossoff / d$wz_total
  
  #Internal preference votes
  
  d$intvt_perc <- d$votes_own/d$WZretour
  d$intvt_perc_r <- d$intvt_perc/2
  
  #Define Variables as Factors
  
  cols <- c("list_id", "nonswissnames", "incumbent", "precumulated", 
            "sex_d", "age_c3", "party_c3", "commune",
            "origincode_c3", "CodeBerufK3", "nonswissname_lang")
  
  d[cols] <- lapply(d[cols], factor)
  
#---------------- SAVE DATA

save(d, file = ".../PrefVotes")
